package com.edu.dao;

import com.edu.pojo.EduCurriculum;
import com.edu.pojo.EduDept;

import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * 分院/班级数据层
 *
 * @author YJF
 */
public interface EduDeptMapper extends Mapper<EduDept> {

    /**
     * 根据ID查找部门
     *
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Select("SELECT " +
            "d.dept_id, parent_id, dept_name, d.phone, d.email, d.status, tec_name, tec_per_id " +
            "FROM edu_dept d LEFT JOIN edu_teacher t " +
            "ON d.tec_id = t.tec_id WHERE d.dept_id = #{deptId}")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "dept_name", property = "deptName"),
            @Result(column = "tec_name", property = "leader"),
            @Result(column = "tec_per_id", property = "leaderId")
    })
    EduDept selectById(Integer deptId);

    /**
     * 根据部门名称查找部门
     *
     * @param deptName 部门ID
     * @return 部门信息
     */
    @Select("SELECT COUNT(*) FROM edu_dept WHERE dept_name = #{deptName};")
    int selectByName(String deptName);

    /**
     * 分页条件查询班级
     *
     * @param parent 分院
     * @param cls    班级
     * @return 结果列表
     */
    @Select("SELECT " +
            "c.*, p.dept_name parent_dept, t.tec_name " +
            "FROM edu_dept c LEFT JOIN edu_dept p " +
            "ON p.dept_id = c.parent_id LEFT JOIN edu_teacher t ON c.tec_id = t.tec_id " +
            "WHERE c.parent_id != 0 AND p.dept_name LIKE '%${parent}%' AND c.dept_name LIKE '%${cls}%'")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "dept_name", property = "deptName"),
            @Result(column = "tec_id", property = "tecId"),
            @Result(column = "tec_name", property = "leader"),
            @Result(column = "parent_dept", property = "parentDept")
    })
    List<EduDept> findCls(String parent, String cls);

    /**
     * 查询所有分院
     *
     * @return 分院列表
     */
    @Select("SELECT * FROM edu_dept WHERE parent_id = 0 AND status = 1")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "dept_name", property = "deptName"),
    })
    List<EduDept> findAllDept();

    /**
     * 根据分院ID查找班级列表
     *
     * @param deptId 分院ID
     * @return 班级列表
     */
    @Select("SELECT * FROM edu_dept WHERE parent_id = #{deptId}")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "dept_name", property = "deptName"),
    })
    List<EduDept> findAllClsById(Integer deptId);

    /**
     * 根据教师ID清空部门的教师ID信息
     *
     * @param tecId 教师ID
     */
    @Update("UPDATE edu_dept SET tec_id = '' WHERE tec_id = #{tecId}")
    void clearTecId(Long tecId);

    /**
     * 根据课程ID搜寻选课班级
     *
     * @param id 课程ID
     * @return 选课班级
     */
    @Select("SELECT d.dept_id, dept_name " +
            "FROM edu_curriculum c LEFT JOIN edu_cur_cls cls ON" +
            " c.cur_id = cls.cur_id LEFT JOIN edu_dept d ON cls.cls_id = d.dept_id WHERE c.cur_id = #{id}")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "dept_name", property = "deptName")
    })
    List<EduDept> findClsAndCur(@Param("id") Long id);
}
